from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from recruit.models import Recruit, Enterprise, City

# 获取职位序列化器
from recruit.search_indexes import EnterpriseIndexes
from users.models import User


class RecruitSerializer(ModelSerializer):
    class Meta:
        model = Recruit
        fields = '__all__'


#  嵌套企业序列化器
class RecruitEnterprisSerializer(ModelSerializer):
    class Meta:
        model = Recruit
        # fields = ['id']
        fields = '__all__'


# 获取企业序列化器
class EnterpriseSerializer(ModelSerializer):
    recruits = RecruitEnterprisSerializer(many=True)
    users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    class Meta:
        model = Enterprise
        fields = ['id', 'labels', 'url','content', 'logo', 'name', 'recruits', 'summary', 'users']


class RecruitInfoUserSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'


class RecruitInfoSerializer(ModelSerializer):
    enterprise = EnterpriseSerializer()
    users = RecruitInfoUserSerializer(many=True)

    class Meta:
        model = Recruit
        fields = '__all__'


# class RecruitIndexSerializer(HaystackSerializer):
#     class Meta:
#         index_classes = [EnterpriseIndexes]
#         fields = '__all__'


# =================================职位搜索======================================
class EnterpriseModelSerializer(serializers.ModelSerializer):
    """企业序列化器"""
    recruits = serializers.StringRelatedField(label='职位')

    class Meta:
        model = Enterprise
        fields = ("id", "name", "labels", "logo", 'recruits', "summary")


class RecuitModelSerialzier(serializers.ModelSerializer):
    """职位序序列化器"""
    enterprise = EnterpriseModelSerializer(label='企业')
    class Meta:
        model = Recruit
        fields = ("id", "jobname", "salary", "condition", "education",
                  "type", "city", "createtime", 'enterprise', "labels")

# =================================职位搜索======================================

class HotCitySerializer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields = '__all__'

